﻿using UnityEngine;
using System.Collections;

namespace Room
{
    public class PlayerAIStateDeath : PlayerAIStateBase
    {
        GameObject obj;
        GameObject light0;
        protected override void OnEnter()
        {
            //由服务端发逮捕状态
            //if (StaticData.playerData.playerId == long.Parse(controller.owner.name))
            //{
                //var killerNum = controller.owner.GetKillNum();
                //var killer = Dict.Blo.DictKillerBlo.GetKillerData(killerNum);
                //if (killer.jailFlag == 1)
                //{
                //   var ai = controller.owner.ai as RCAIActor;
                //   ai.SwitchState(PlayerAIState.Arress);
                //}
                //else
                //{
                //    AskRebornBox();
                //}
            //}

            if(obj!=null)
            {
                GameObjectPoolManager.Instance.Unspawn(obj);
            }

            if(light0!=null)
            {
                GameObjectPoolManager.Instance.Unspawn(light0);
            }

            obj = GameObjectPoolManager.Instance.Spawn(RoomField.particleDieAb,CommonMethod.GetAssetNameFromAssetBundleName(RoomField.particleDieAb));
            obj.transform.SetParent(controller.owner.transform);
            obj.transform.localScale = Vector3.one;
            obj.transform.localPosition = Vector3.zero;
            //obj.transform.SetParent(controller.owner.transform.parent);
            obj.SetActive(true);

            light0 = GameObjectPoolManager.Instance.Spawn(RoomField.particleLitAb, CommonMethod.GetAssetNameFromAssetBundleName(RoomField.particleLitAb));
            light0.transform.SetParent(controller.owner.transform);
            light0.transform.localScale = Vector3.one;
            light0.transform.localPosition = Vector3.zero;
            //light0.transform.SetParent(controller.owner.transform.parent);
            light0.SetActive(true);

            controller.owner.SetInView(false);

            timer = 1f;
            currTime = 0f;
        }

        protected override void OnLeave()
        {
            if (obj != null)
                GameObjectPoolManager.Instance.Unspawn(obj);
            if (light0 != null)
                GameObjectPoolManager.Instance.Unspawn(light0);
            controller.owner.charactorManager.GetSelfCharactor().SetInView(true);

        }


        protected override void OnTick()
        {
            base.OnTick();

            currTime += Time.deltaTime;
            if(currTime >= timer)
            {
                CheckAskRebornBoxExist();
                currTime = 0f;
            }
        }

        float timer = 1f;
        float currTime = 0f;


        void CheckAskRebornBoxExist()
        {
            var currPage = UI.PageManager.Instance.currentPage as UI.UIFieldRunMap;
            if(currPage!=null && 
                !currPage.askEaster.gameObject.activeSelf && 
                StaticData.playerData.playerId == long.Parse(controller.owner.name))
            {
                AskRebornBox();
            }
        }

        void AskRebornBox()
        {
            var room = Room.RoomManager.currentRoom as RoomField;
            if (room != null)
            {
                var netManager = (RoomNetField)room.netManager;
                UI.UIFieldRunMap.AskReborn(netManager.SendSafeAreaReborn, netManager.SendReborn);
            }
        }
    }
}